﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MobileTipper
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // Tip.Test();

            Tip.Clear();
            DisplayTip();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemClear_Click(object sender, EventArgs e)
        {
            Tip.Clear();
            DisplayTip();
        }

        private void menuItemCalculate_Click(object sender, EventArgs e)
        {
            StringBuilder message = new StringBuilder();

            if (textBoxAmountOfBill.DecimalValue < 0)
                message.Append("Amount of bill must be greater than 0.00.").AppendLine(); ;
            if (comboBoxTaxRate.DecimalValue < 0)
                message.Append("Tax rate must be greater than 0.00%.").AppendLine(); ;
            if (comboBoxTipRate.DecimalValue < 0)
                message.Append("Tip rate must be greater than 0.00%.").AppendLine(); ;
            if (comboBoxNumberInParty.IntValue <= 0)
                message.Append("Number in party must be greater than 0.").AppendLine(); ;

            if (message.ToString().Length > 0)
            {
                MessageBox.Show(message.ToString(), "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            Tip.AmountOfBill = textBoxAmountOfBill.DecimalValue;
            Tip.TaxRate = comboBoxTaxRate.DecimalValue;
            Tip.TipRate = comboBoxTipRate.DecimalValue;
            Tip.NumberInParty = comboBoxNumberInParty.IntValue;

            Tip.Calculate();

            DisplayTip();
        }

        private void DisplayTip()
        {
            textBoxAmountOfBill.Text = Tip.AmountOfBill.ToString("c");
            comboBoxTaxRate.Text = (Tip.TaxRate/100).ToString("0%");
            textBoxAmountOfSubtotal.Text = Tip.AmountOfSubtotal.ToString("c");
            comboBoxTipRate.Text = (Tip.TipRate/100).ToString("0%");
            textBoxAmountOfTotal.Text = Tip.AmountOfTotal.ToString("c");
            comboBoxNumberInParty.Text = Tip.NumberInParty.ToString();
            textBoxAmountOfTax.Text = Tip.AmountOfTax.ToString("c");
            textBoxAmountOfTip.Text = Tip.AmountOfTip.ToString("c");
            textBoxIndividualShare.Text = Tip.IndividualShare.ToString("c");
        }
    }
}